"use strict";
const path = require("path");

function resolve(dir) {
  return path.join(__dirname, dir);
}

module.exports = {
  publicPath: "/", // 打包路径
  outputDir: "dist", // 输出文件夹，默认dist
  assetsDir: "static", // 静态文件地址
  lintOnSave: process.env.NODE_ENV === "development",
  productionSourceMap: true, // production 关闭sourceMap
  configureWebpack: {
    resolve: {
      alias: {
        "@": resolve("src")
      },
      extensions: [".js", ".vue", ".ts"]
    }
  },
  devServer: {
    port: 10000
  },
  configureWebpack: {
    plugins:
      process.env.NODE_ENV !== "development"
        ? [
            //  添加plugin
          ]
        : []
  },
  chainWebpack(config) {
    config.entry.app = ["@babel/polyfill", "./src/main.js"];
    config.plugins.delete("preload"); // TODO: need test
    config.plugins.delete("prefetch"); // TODO: need test
    // 设置svg-loader
    config.module
      .rule("svg")
      .exclude.add(resolve("src/icons"))
      .end();
    config.module
      .rule("icons")
      .test(/\.svg$/)
      .include.add(resolve("src/icons"))
      .end()
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .options({
        symbolId: "icon-[name]"
      })
      .end();
    // set preserveWhitespace
    // 目的是为了去掉元素间的空格
    config.module
      .rule("vue")
      .use("vue-loader")
      .loader("vue-loader")
      .tap(options => {
        options.compilerOptions.preserveWhitespace = false;
        return options;
      })
      .end();

    config
      // https://webpack.js.org/configuration/devtool/#development
      // sourceMap 可以显示出原始代码 cheap-module-eval-source-map
      .when(process.env.NODE_ENV === "development", config =>
        config.devtool("cheap-module-eval-source-map")
      );

    config.when(process.env.NODE_ENV !== "development", config => {
      config
        .plugin("ScriptExtHtmlWebpackPlugin")
        .after("html")
        .use("script-ext-html-webpack-plugin", [
          {
            // 注意一定要在HtmlWebpackPlugin之后引用
            // inline 的 name 和你 runtimeChunk 的 name保持一致
            inline: /runtime\..*\.js$/
          }
        ])
        .end();
      // config
      //   .plugin('webpack-bundle-analyzer')
      //   .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
      //   .end()
      config.optimization.splitChunks({
        chunks: "all",
        cacheGroups: {
          // 基础类库单独打包
          libs: {
            name: "chunk-libs",
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: "initial" // 只打包初始时依赖的第三方包
          },
          // ui框架单独抽离
          iview: {
            name: "chunk-iview", // 单独拆分 iview
            priority: 20, // 权重项；权重要大于libs和app，不然会被打包进libs或者app中
            test: /[\\/]node_modules[\\/]_?iview(.*)/
          },
          //自定义组件函数抽离
          commons: {
            name: "chunk-commons",
            test: resolve("src/components"), // 自定义拓展规则
            minChunks: 3, //  最小共用次数
            priority: 5,
            reuseExistingChunk: true
          }
        }
      });
      // 生成 manifest.json
      // 它的作用是将包含 chunks 映射关系的 list单独从 app.js里提取出来
      config.optimization.runtimeChunk("single");
    });
  }
};
